package net.yegong;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import net.yegong.mva.ComputingException;

public class ConsoleMain {

	public static void main(String[] argv) throws Exception {
		Reader reader = read();
		source(reader);
		zip(reader);
		unzip();
	}

	public static Reader read() throws Exception {
		Logger.log("--- Start reading ---");
		Reader reader = new Reader(new File("data\\band1.img"), 10000);
		reader.read();
		return reader;
	}

	public static void source(Reader reader) throws IOException {
		Logger.log("--- Start source ---");
		OutputStream os = new BufferedOutputStream(new FileOutputStream("output\\source.bin"));
		byte[] b = reader.getData();
		os.write(b);
		os.flush();
		os.close();
	}

	public static void zip(Reader reader) throws IOException, ComputingException {
		Logger.log("--- Start zip ---");
		OutputStream fos = new FileOutputStream("output\\zip.bin");
		FileTypeCheck.check(fos);
		ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(new GZIPOutputStream(fos)));

		Zip.zip(reader, os, 0.99f);
		os.close();
	}

	public static void unzip() throws IOException {
		Logger.log("--- Start unzip ---");
		InputStream fis = new FileInputStream("output\\zip.bin");
		FileTypeCheck.check(fis);
		ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new GZIPInputStream(fis)));
		OutputStream os = new BufferedOutputStream(new FileOutputStream("output\\unzip.bin"));

		Unzip.unzip(is, os);
		is.close();
		os.close();

	}
}
